/*
 * Copyright 2016-2019 yoara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package yhao.infra.feature.oracle;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.type.JdbcType;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.scheduling.concurrent.ScheduledExecutorFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import yhao.infra.feature.oracle.mybatis.dialect.OracleDialect;
import yhao.infra.service.YhaoInfraServiceProperties;

import java.util.concurrent.ScheduledExecutorService;

/**
 * Oracle 配置类
 *
 * @author yoara
 */
@ComponentScan("yhao.infra.feature.oracle.mybatis")
@EnableConfigurationProperties(YhaoOracleProperties.class)
public class OracleConfig {
    @Bean(name="oracleCreateScheduler")
    public ScheduledExecutorFactoryBean oracleCreateScheduler(YhaoOracleProperties properties){
        ScheduledExecutorFactoryBean scheduledExecutorFactoryBean = new ScheduledExecutorFactoryBean();
        scheduledExecutorFactoryBean.setPoolSize(properties.getDruidScheduledExecutorPoolSize());
        return scheduledExecutorFactoryBean;
    }

    @Bean(name="oracleDefaultDataSource",initMethod = "init",destroyMethod = "close")
    public DruidDataSource oracleDefaultDataSource(YhaoOracleProperties properties,
            ScheduledExecutorService oracleCreateScheduler){
        return ConfigFactory.makeOracleDruidDataSource(properties, oracleCreateScheduler);
    }

    @Bean(name="oracleDataSource")
    public DruidDataSource oracleDataSource(DruidDataSource oracleDefaultDataSource){
        DruidDataSource defaultDataSource = oracleDefaultDataSource;
        //多库切换
        //DynamicDataSource source = new DynamicDataSource();
        //Map<Object, Object> targetDataSources =
        //        ImmutableMap.of("defaultDataSource",defaultDataSource);
        //source.setTargetDataSources(targetDataSources);
        //source.setDefaultTargetDataSource(defaultDataSource);

        return defaultDataSource;
    }

    @Bean(name="oracleSqlSessionFactory")
    public SqlSessionFactory oracleSqlSessionFactory(
            DruidDataSource oracleDataSource, YhaoInfraServiceProperties baseProperties) throws Exception {
        SqlSessionFactory factory = ConfigFactory.makeSqlSessionFactory(baseProperties,oracleDataSource, OracleDialect.class);
        factory.getConfiguration().setJdbcTypeForNull(JdbcType.NULL);
        return factory;
    }

    @Bean(name="oracleJdbcTemplate")
    public JdbcTemplate oracleJdbcTemplate(DruidDataSource oracleDataSource){
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(oracleDataSource);
        return jdbcTemplate;
    }

    @Bean(name = "oracleTxManager")
    public PlatformTransactionManager oracleTxManager(DruidDataSource oracleDataSource) {
        return new DataSourceTransactionManager(oracleDataSource);
    }
}
